package com.tiwi.gmbdr;


//import java.util.List;



import com.tiwi.gmbdr.tools.*;
import com.tiwi.gmbdr.R;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnItemClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 GridView gridview = (GridView) findViewById(R.id.gridview);
		 
	        // Instance of ImageAdapter Class

		    gridview.setAdapter(new ImageAdapter(this));
		    gridview.setOnItemClickListener(this);
		    
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	//	getMenuInflater().inflate(R.menu.activity_main, menu);
	
		menu.add(getString(R.string.update_soal));
		menu.add(getString(R.string.exit));
		return true;
	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    String title = item.getTitle().toString();
	    if (getString(R.string.update_soal).equals(title)) {
	    	UpdateSoalTask task = new UpdateSoalTask(this);
	    	task.execute();
	    }
	    else if (getString(R.string.exit).equals(title)) {
	    	
	    	DialogManager.showExitAlert(this, getString(R.string.alertExit), true);
	    }
	    
	    
	    return super.onOptionsItemSelected(item);		
	}
	
	
	private class ImageAdapter extends BaseAdapter {
		private Context context;
		
		private String[] menu = {
				getString(R.string.menuPermainanBaru),
				getString(R.string.menuLanjutkanPermainan),
				getString(R.string.menuSkortertinggi)
		};
		
		private int[] menuIcon = {
				R.drawable.ic_svy_list,
				R.drawable.ic_svy_new,
				R.drawable.ic_svy_sent,
		};
		
		
		public ImageAdapter(Context c) {
			this.context = c;
		}
		
		public int getCount() {
			return menu.length;
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int arg0) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
		    View v=null;
	
		    
		    	LayoutInflater inflater = getLayoutInflater();
		    	v = inflater.inflate(R.layout.grid_menu, null);
				TextView tv = (TextView)v.findViewById(R.id.icon_text);
				tv.setText(menu[position]);
				ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
				iv.setImageResource(menuIcon[position]);

		    return v;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		// TODO Auto-generated method stub
		if( position == 0){
			Intent i = new Intent(this, NewProfileActivity.class);
			this.startActivity(i);
		}
	}

}
